//
//  CubeModuleParser.m
//  Chamleon-template
//
//  Created by Fanty on 15/12/14.
//
//

#import "CubeModuleParser.h"
#import "Modules.h"
#import "DataCenter.h"


@implementation CubeModuleParser

+(NSString*)serialize:(CubeModel*)model {
    NSMutableString* str=[[NSMutableString alloc] initWithCapacity:1];
    [str appendString:@"{"];
    
    [str appendFormat:@"\"iconUrl\":\"%@\",",[model.iconUrl length]>0?model.iconUrl:@"" ];
    
    [str appendFormat:@"\"build\":%d,",model.build ];
    [str appendFormat:@"\"version\":\"%@\",",model.version];
    
    [str appendFormat:@"\"name\":\"%@\",",[model.name stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]];
    [str appendFormat:@"\"identifier\":\"%@\",",model.identifier];

    [str appendFormat:@"\"platform\":\"%@\",",[model.platform length]>0?model.platform:@""];
    
    [str appendFormat:@"\"moduleUrl\":\"%@\",",[model.moduleUrl length]>0?model.moduleUrl:@""];

    NSString* releaseNote=[model.releaseNote stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    [str appendFormat:@"\"releaseNote\":\"%@\",",[releaseNote length]>0?releaseNote:@""];
    [str appendFormat:@"\"moduleType\":%d,",model.moduleType];
    [str appendFormat:@"\"downloadedProcess\":%ld,",((model.moduleType==CubeMoudleStatusFinish || model.moduleType==CubeMoudleStatusCanUpdate)?model.downloadedTotalCount:model.downloadedProcess) ];
    [str appendFormat:@"\"downloadedTotalCount\":%ld",model.downloadedTotalCount ];
    
    [str appendString:@"}"];
    
    return str;
}

+(NSString*)parserJSONFromArray:(NSArray*)array {
    //修改解决主模块标识符
    CubeModel* mainModule=[[DataCenter defaultCenter] findMainCubeModule];
    NSMutableString* str=[[NSMutableString alloc] initWithCapacity:1];
    [str appendFormat:@"{\"mainModuleTag\":\"%@\",",mainModule.identifier];
    [str appendString:@"\"list\":["];
    [array enumerateObjectsUsingBlock:^(id obj,NSUInteger index,BOOL* stop){
        @autoreleasepool {
            CubeModel* model=(CubeModel*)obj;
            [str appendString:[CubeModuleParser serialize:model]];
            if(index<[array count]-1)
                [str appendString:@","];
        }
    }];
    [str appendString:@"]}"];
    
    return str;
}

@end

